from django.core.management.base import BaseCommand
from django.contrib.auth.models import User, Group

class Command(BaseCommand):
    help = '创建测试用户'

    def handle(self, *args, **options):
        # 创建图书管理员用户
        librarian, created = User.objects.get_or_create(
            username='librarian',
            email='librarian@example.com'
        )
        if created:
            librarian.set_password('librarian123')
            librarian.save()
            librarian_group = Group.objects.get(name='Librarian')
            librarian.groups.add(librarian_group)
            self.stdout.write('成功创建图书管理员用户')

        # 创建普通用户
        member, created = User.objects.get_or_create(
            username='member',
            email='member@example.com'
        )
        if created:
            member.set_password('member123')
            member.save()
            member_group = Group.objects.get(name='Member')
            member.groups.add(member_group)
            self.stdout.write('成功创建普通用户')

        self.stdout.write(self.style.SUCCESS('测试用户创建完成！'))
        self.stdout.write('图书管理员账号：librarian / librarian123')
        self.stdout.write('普通用户账号：member / member123') 